import { hilog } from '@kit.PerformanceAnalysisKit';

const IAP_LOG_DOMIN = 0x0ffe;

export default class Logger {
  static prefix: string = '[IAP_Demo]';

  public static setPrefix(module: string): void {
    this.prefix = '[IAP_Demo.' + module + '] ';
  }

  public static log(message, ...args: any[]): void {
    hilog.info(IAP_LOG_DOMIN, this.prefix, message, args);
  }

  public static info(message, ...args: any[]): void {
    hilog.info(IAP_LOG_DOMIN, this.prefix, message, args);
  }

  public static debug(message, ...args: any[]): void {
    hilog.debug(IAP_LOG_DOMIN, this.prefix, message, args);
  }

  public static warn(message, ...args: any[]): void {
    hilog.warn(IAP_LOG_DOMIN, this.prefix, message, args);
  }

  public static error(message, ...args: any[]): void {
    hilog.error(IAP_LOG_DOMIN, this.prefix, message, args);
  }

  public static err(message, ...args: any[]): void {
    hilog.error(IAP_LOG_DOMIN, Logger.prefix, message, args);
  }

  public static log4Json(message, ...args: any[]): void {
    let args4Json = []
    for (let arg of args) {
      args4Json.push(JSON.stringify(arg))
    }
    hilog.debug(IAP_LOG_DOMIN, this.prefix, message + args4Json)
  }

  public static printJsonObj(obj): void {
    const str = JSON.stringify(obj, null, 4);
    for (let i = 0; i < str.length / 512; i++) {
      hilog.debug(IAP_LOG_DOMIN, this.prefix, str.slice(i * 512, (i + 1) * 512));
    }
  }
}
